package com.jiai.cloud.sys.controller;



import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.redis.util.RedisUtil;
import com.jiai.cloud.base.tools.RestResultGenerator;
import com.jiai.cloud.base.util.ValidateCode;
import com.jiai.cloud.entity.TUser;
import com.jiai.cloud.sys.service.ITUserService;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Created by 佳爱科技 on 2017/5/25.
 */
@RestController
@RequestMapping("/gold")
public class GoldController {
    private final static String CACH_KEY="email_code";
    @Autowired
    private ITUserService itUserService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private JavaMailSender mailSender; //自动注入的Bean
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;  //自动注入

    @Value("${spring.mail.username}")
    private String Sender; //读取配置文件中的参数

    //邮箱验证
    @RequestMapping(value = "/chekcEmail",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public Object chekcEmail(String email,String code) {
            String  oldCode=redisUtil.hashGet(0,CACH_KEY,email);
            if(StringUtils.isEmpty(oldCode)){
                return RestResultGenerator.genErrorResult("超时");
            }else if(!oldCode.equals(code)){
                return RestResultGenerator.genErrorResult("失败");
        }else{
                return RestResultGenerator.genSuccessResult("验证码正确");
            }

    }

    //邮箱发送
    @RequestMapping(value = "/sendEmailCode" ,method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public  Object sendEmail(@RequestParam("email") String email){
           MimeMessage message = null;
           try {
               message = mailSender.createMimeMessage();
               MimeMessageHelper helper = new MimeMessageHelper(message, true);
               helper.setFrom(Sender);
               helper.setTo(email);
               helper.setSubject("邮箱验证码");
               Map<String, Object> model = new HashMap();
               Random ne = new Random();//实例化一个random的对象ne
               int x = ne.nextInt(9999 - 1000 + 1) + 1000;
               String num = "" + x;
               redisUtil.hashSet(0, CACH_KEY, email, num);
               model.put("code", num);
               //读取 html 模板
               Template template = freeMarkerConfigurer.getConfiguration().getTemplate("emial_code.html");
               String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
               helper.setText(html, true);
             //  mailSender.send(message);
               return RestResultGenerator.genSuccessResult("验证码发送成功");
           } catch (Exception e) {
               return RestResultGenerator.genErrorResult("邮箱发送失败");
           }


     }

}
